package com.fan.shop.services;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.transaction.annotation.Transactional;

import com.fan.shop.dao.StockDao;
import com.fan.shop.domain.Stock;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.StockUserClass;

@Named("stockService")
@Transactional(readOnly = true)
public class StockServiceImpl implements StockService {

	@Inject
	@Named("stockDao")
	private StockDao stockDao;

	private String storeCode;

	public StockDao getStockDao() {
		return stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	@SuppressWarnings("rawtypes")
	public List getList() {
		if (storeCode == null || storeCode.trim().length() == 0) {
			List<StockUserClass> list = new ArrayList<StockUserClass>();
			for (Stock stock : stockDao.getAllStocks()) {
				list.add(copy(stock));
			}
			return list;
		} else {
			List<StockUserClass> list = new ArrayList<StockUserClass>();
			for (Stock stock : stockDao.getStocksByStore(storeCode)) {
				list.add(copy(stock));
			}
			return list;
		}

	}

	public int getColumnsSize() {
		return stockDao.getColumnNames().size();
	}

	public StockUserClass copy(Object obj) {
		Stock stock = (Stock) obj;
		StockUserClass stockUserClass = new StockUserClass();
		stockUserClass.setItemCode(stock.getId().getItemCode());
		stockUserClass.setStoreCode(stock.getId().getStoreCode());
		stockUserClass.setCost(stock.getCost());
		stockUserClass.setNetPrice(stock.getNetPrice());
		stockUserClass.setQuantity(stock.getQuantity());
		stockUserClass.setHoldQuantity(stock.getHoldQty());

		stockUserClass.setItemIcon("<img class='thumbnail' src='/"
				+ ShopGlobalVariables.MODULE_NAME
				+ ShopGlobalVariables.PATH_SEPERATOR
				+ ShopGlobalVariables.ICON_PATH + stock.getId().getItemCode()
				+ ShopGlobalVariables.JPEG_SUFFIX
				+ "' height='42' width='42' />");

		return stockUserClass;
	}

	public Stock getStock(String itemCode, String storeCode) {
		return stockDao.getStock(itemCode, storeCode);
	}

	public List<Stock> getAllStocks() {
		return stockDao.getAllStocks();
	}

	public int getSize() {
		return stockDao.getSize();
	}

	@Transactional(readOnly = false)
	public void addStock(Stock stock) {
		stockDao.addStock(stock);

	}

	@Transactional(readOnly = false)
	public void updateStock(Stock stock) {
		stockDao.updateStock(stock);
	}

	@Transactional(readOnly = false)
	public void deleteStock(Stock stock) {
		stockDao.deleteStock(stock);
	}

	public boolean hasStock(String itemCode, String storeCode) {
		return stockDao.hasStock(itemCode, storeCode);
	}

	public List<String> getColumnNames() {
		return stockDao.getColumnNames();
	}

	public List<Stock> getStocksByStore(String storeCode) {
		return stockDao.getStocksByStore(storeCode);
	}

	public String getStoreCode() {
		return storeCode;
	}

	public void setStoreCode(String storeCode) {
		this.storeCode = storeCode;
	}

}
